package edu.gatech.cs2340.sharks.fms;

/**
 * AddAdminActivity.java
 * @author Steven Mitchell
 * 
 * Creates a new user in the database that has adminstrative powers, rather than client powers.
 * If successful, returns to admin home activity.
 */

import edu.gatech.cs2340.sharks.fms.data.*;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.TextView;

public class AddAdminActivity extends Activity
{
	/**
	 * The success message that is sent to the admin home upon a successful admin creation.
	 */
	public final static String SUCCESS_MESSAGE = "edu.gatech.cs2340.sharks.fms.SUCCESS_MESSAGE";

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_add_admin);
	}

	/**
	 * This activity obtains all of the user input from textfields, converts them to strings,
	 * and creates an admin. If succesful, the activity finishes and you return to the admin home.
	 * Else, an error is displayed.
	 * 
	 * @param button The button clicked that starts the activity
	 */
	public void createAdmin(View button)
	{
		final EditText nameField = (EditText) findViewById(R.id.editTextName);
		String name = nameField.getText().toString();
		final EditText userField = (EditText) findViewById(R.id.editTextUsername);
		String username = userField.getText().toString();
		final EditText passField = (EditText) findViewById(R.id.editTextPass);
		String password = passField.getText().toString();
		Status success = DBMgr.createUser(username, password, name, User.Role.ADMIN, "admin@example.com", "Nowhere", "Sample profile");
		final TextView statusField = (TextView) findViewById(R.id.textViewStatus);
		if (success.code() == Status.Code.OK)
		{
			Intent intent = new Intent(this, AdminHomeActivity.class);
			intent.putExtra(SUCCESS_MESSAGE, "You have successfully created an admin!"); 
			startActivity(intent);
			finish();
		}
		else
		{
			statusField.setText(success.toString());
		}
	}
}
